<?php
/***************************************************************************
 *                             roster.php
 *                            -------------------
 *                       (APU) Automated Pilot Utility
 *   begin                : Tuesday, Nov 29, 2004 : Sunday Nov 5, 2006
 *   copyright            : (C) 2004-2006 SF Games
 *   email                : techsupport@sf-games.com
 ****************************************************************************/

	include('include/common.php');
	include('include/config.php');
	include('include/dbcommon.php');
	include('include/randomQuote.php');

	//
	// Initialize variables
	//

	// Format up the number of decimal places user wants for display of flight hours
	$format = "%2." .$iNumDecimals ."f";

	$monthTotalsArray = $PirepArray;
	$yearTotalsArray  = $PirepArray;

	// Page generation timer start.
	$time = microtime();
	$time = explode(" ", $time);$time = $time[1] + $time[0];$start = $time;

	htmlHeader();

	//
	// Database Connect
	//

	$db = mysql_connect($dbhost, $dbuname, $dbpass);

	if ( !$db )
	{
		echo "Fatal Error: Could not connect to the SQL server<br>";
		echo "Try again in a few minutes. If problem continues, contact the webmaster of this site.<br>";
	}
	else
	{
		mysql_select_db($dbname);

		//
		// Date Time
		//

		$today  = getdate();
		$sMonth = $today["month"];
		$sYear  = $today["year"];
		$sToday = $sMonth ." " .$sYear;

		//
		// Build the roster
		//

		headerTable($vaName, $today["month"], $today["year"]);

		//
		// Active Pilots Table
		//

		activePilotsTableBegin();
			// Query for all active pilots (not on LOA and not deleted)
			// Note: ORDER BY PILOT_ID future versions will allow for user sorting.
			$query  = "SELECT * FROM " .$table_prefix ."PILOTS WHERE ACTIVE = '1' AND LOA = '0' AND DELETED = '0' ORDER BY PILOT_ID";
			$pilots = mysql_query($query);
			if ( !$pilots )
				die("Query for active pilots failed.<br>Click your browsers Refresh button to try again.<br>If you continue to receive this error, contact the webmaster<br>Failed with mySQL error: " . mysql_error());

			//
			// For each Pilot
			//

			while ($pilot = mysql_fetch_array($pilots, MYSQL_ASSOC))
			{
				$pirepInfo = gatherPilotsPirepsByDate($pilot["PILOT_ID"], getBegMonth(), getTodayAsSQLDate());
				$pilotName = $pilot["FIRST_NAME"] ." " .$pilot["LAST_NAME"];
				$pilotID   = $pilot["PILOT_ID"];
				$rankGfx   = buildRankGraphics($pilot["RANK_AC"], $pilot["HOURS_HELO"], $pilot["RANK_HELO"], $pilot["DART"], $pilot["SAR"], $pilot["FLEET_SUP"], $bUseAwards);
				$numPireps = $pirepInfo["PIREPS"];
				$monthHrs  = sprintf($format, $pirepInfo["FLIGHT_HRS"]);
				$acHrs     = sprintf($format, $pilot["HOURS_AC"]);
				$heloHrs   = sprintf($format, $pilot["HOURS_HELO"]);
				$totalHrs  = sprintf($format, $pilot["TOTAL_HRS"]);
				$comments  = $pirepInfo["COMMENTS"];

				// Add the pilot to the table
				activePilotsTableAddRecord($pilotName, $pilotID, $rankGfx, $numPireps, $monthHrs, $acHrs, $heloHrs, $totalHrs, $comments);

				// Sum up the pilots record for month totals
				$monthTotalsArray["PILOTS"]++;
				$monthTotalsArray["PIREPS"]      += $pirepInfo["PIREPS"];
				$monthTotalsArray["PAX"]         += $pirepInfo["PAX"];
				$monthTotalsArray["CARGO"]       += $pirepInfo["CARGO"];
				$monthTotalsArray["FUEL_BURNED"] += $pirepInfo["FUEL_BURNED"];
				$monthTotalsArray["FLIGHT_HRS"]  += $pirepInfo["FLIGHT_HRS"];
			}
		activePilotsTableEnd();

		//
		// Current Month Totals Table
		//

		if ( $bDisplayTotalsTable )
		{
			$yearTotalsArray = gatherPirepsByDate(getBegYear(), getTodayAsSQLDate());
			totalsTable($monthTotalsArray, $yearTotalsArray);
		}

		//
		// Pilots on Leave of Absence (LOA) Table
		//

		if ( $bDisplayLOATable )
		{
			loaPilotsTableBegin();
				// Query for all loa pilots
				// Note: ORDER BY PILOT_ID future versions will allow for user sorting.
				$query  = "SELECT * FROM " .$table_prefix ."PILOTS WHERE LOA = '1' AND DELETED = '0' ORDER BY PILOT_ID";
				$pilots = mysql_query($query);
				if ( !$pilots )
					die("Query for loa pilots failed.<br>Click your browsers Refresh button to try again.<br>If you continue to receive this error, contact the webmaster<br>Failed with mySQL error: " . mysql_error());

				//
				// For each Pilot
				//

				while ($pilot = mysql_fetch_array($pilots, MYSQL_ASSOC))
				{
					$pilotName = $pilot["FIRST_NAME"] ." " .$pilot["LAST_NAME"];
					$pilotID   = $pilot["PILOT_ID"];
					$rankGfx   = buildRankGraphics($pilot["RANK_AC"], $pilot["HOURS_HELO"], $pilot["RANK_HELO"], $pilot["DART"], $pilot["SAR"], $pilot["FLEET_SUP"], $bUseAwards);
					$acHrs     = sprintf($format, $pilot["HOURS_AC"]);
					$heloHrs   = sprintf($format, $pilot["HOURS_HELO"]);
					$totalHrs  = sprintf($format, $pilot["TOTAL_HRS"]);

					// Add the pilot to the table
					loaPilotsTableAddRecord($pilotName, $pilotID, $rankGfx, $acHrs, $heloHrs, $totalHrs);
				}
			loaPilotsTableEnd();
		}

		//
		// Inactive Pilots Table
		//

		if ( $bDisplayInactiveTable )
		{
			inactivePilotsTableBegin();
				// Query for all inactive pilots
				// Note: ORDER BY PILOT_ID future versions will allow for user sorting.
				$query  = "SELECT * FROM " .$table_prefix ."PILOTS WHERE ACTIVE = '0' AND DELETED = '0' ORDER BY PILOT_ID";
				$pilots = mysql_query($query);
				if ( !$pilots )
					die("Query for inactive pilots failed.<br>Click your browsers Refresh button to try again.<br>If you continue to receive this error, contact the webmaster<br>Failed with mySQL error: " . mysql_error());

				//
				// For each Pilot
				//

				while ($pilot = mysql_fetch_array($pilots, MYSQL_ASSOC))
				{
					$pilotName = $pilot["FIRST_NAME"] ." " .$pilot["LAST_NAME"];
					$pilotID   = $pilot["PILOT_ID"];
					$rankGfx   = buildRankGraphics($pilot["RANK_AC"], $pilot["HOURS_HELO"], $pilot["RANK_HELO"], $pilot["DART"], $pilot["SAR"], $pilot["FLEET_SUP"], $bUseAwards);
					$acHrs     = sprintf($format, $pilot["HOURS_AC"]);
					$heloHrs   = sprintf($format, $pilot["HOURS_HELO"]);
					$totalHrs  = sprintf($format, $pilot["TOTAL_HRS"]);

					// Add the pilot to the table
					inactivePilotsTableAddRecord($pilotName, $pilotID, $rankGfx, $acHrs, $heloHrs, $totalHrs);
				}
			inactivePilotsTableEnd();
		}

		//
		// Finish Up, close up HTML and database
		//

		mysql_close($db);
	}

	// End page timing
	$time = microtime();$time = explode(" ", $time);$time = $time[1] + $time[0];
	$finish = $time;$totaltime = ($finish - $start);
	$quote = new RandomQuote;
	printf ("This page %s in %f seconds<br/><br/>", $quote->getQuote(), $totaltime);

	htmlFooter();
?>


<?php

			//////////////////////////////////////////////////
			//                                              //
			//          Roster Support Functions            //
			//                                              //
			//////////////////////////////////////////////////

//
// Display rosters HTML header
//
function htmlHeader()
{
	echo "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n";
	echo "<html xmlns=\"http://www.w3.org/1999/xhtml\">\n";
	echo "<head>\n";
	echo "<title>APU Pilot Roster</title>\n";
	include('include/header.php');
	echo "</head>\n";
	echo "<body>\n";
	echo "<div align=\"center\">\n";
}

//
// Display the rosters HTML footer
//
function htmlFooter()
{
	global $rosterNavigateTo;
	global $rosterNavigateVerbiage;
	global $APU_COPYRIGHT;

	// Display return to another page link/table
	echo "	<table class=\"table_roster_footer\">\n";
	echo "	<tbody>\n";
	echo "		<tr>\n";
	echo "			<td><div align=\"center\"><span class=\"textdark\">Click <a href=\"" .$rosterNavigateTo ."\">here</a> " .$rosterNavigateVerbiage ."</span></div></td>\n";
	echo "		</tr>\n";
	echo "	</tbody>\n";
	echo "	<tfoot><tr><td colspan=\"1\"></td></tr></tfoot>\n";
	echo "	</table>\n";

	// Display Copyright
	echo "<p>" .$APU_COPYRIGHT ."</p>\n";

	// End HTML
	echo "</div>\n";
	echo "</body>\n";
	echo "</html>\n";
}

//
// Display the header Table
//
function headerTable($vaName, $month, $year)
{
	echo "<table class=\"table_roster_header\">\n";
	echo "	<tbody>\n";
	echo "		<tr>\n";
	echo "			<td><div align=\"center\"><span class=\"textredBLrg\">" .$vaName ."<br />Pilot Roster - " .$month ." " .$year ."</span></div></td>\n";
	echo "		</tr>\n";
	echo "	</tbody>\n";
	echo "	<tfoot><tr><td></td></tr>\n";
	echo "	</tfoot>\n";
	echo "</table>\n";
	echo "<p>&nbsp;</p>\n";
}

//
// Begin the Active Pilots Table
//
function activePilotsTableBegin()
{
	global $bFlightTracking;

	echo "<table class=\"table_roster\">\n";
	echo "	<caption>Active Pilots</caption>\n";
	echo "	<thead>\n";
	echo "		<tr>\n";
	echo "			<th>Pilot Name</th><th>Pilot<br />ID</th><th>Rank</th><th>PIREP</th><th>Month<br />Hrs</th><th>A/C<br />Hrs</th><th>Helo<br />Hrs</th><th>Total<br />Hrs</th>";

	if ( $bFlightTracking )
		echo "<th>Online</th>";

	echo "<th>Last Flight Comments</th></tr>\n";

	echo "	</thead>\n";
	echo "	<tbody>\n";
}

//
// Add a row of Pilot information to the Active Pilots Table
//
function activePilotsTableAddRecord($pilotName, $pilotID, $rankGfx, $numPireps, $monthHrs, $acHrs, $heloHrs, $totalHrs, $comments)
{
	global $bFlightTracking;

	echo "		<tr>\n";
	echo "			<td>" .$pilotName ."</td>\n";
	echo "			<td><a href=\"pilotinfo.php?id=" .$pilotID ."\">" .$pilotID ."</a></td>\n";
	echo "			<td width=\"15%\">" .$rankGfx ."</td>\n";
	echo "			<td class=\"tableTextRight\">"     .$numPireps ."</td>\n";
	echo "			<td class=\"tableTextRightRedB\">" .$monthHrs ."</td>\n";
	echo "			<td class=\"tableTextRight\">"     .$acHrs ."</td>\n";
	echo "			<td class=\"tableTextRight\">"     .$heloHrs ."</td>\n";
	echo "			<td class=\"tableTextRight\">"     .$totalHrs ."</td>\n";

	// Show offline until this is all hooked up with VAPN
	if ( $bFlightTracking )
	{
//		if ( $bOnline )
//			echo "			<td class=\"tableTextCenter\"><a href=\"map/FlightTracker.php?id=" .$pilotID ."\"><img src=\"img/notok.jpg\" alt=\"Pilot is not online\" border=\"0\" /></a></td>\n";
//		else
			echo "			<td class=\"tableTextCenter\"><a href=\"map/FlightTracker.php\"><img src=\"img/notok.jpg\" alt=\"Pilot is not online\" border=\"0\" /></a></td>\n";
	}

	echo "			<td width=\"30%\">" .$comments ."</td>\n";
	echo "		</tr>\n";
}

//
// End the Active Pilots Table
//
function activePilotsTableEnd()
{
	global $bFlightTracking;

	$colSpan = 10;
	if ( $bFlightTracking == false)
		$colSpan = 9;

	echo "	</tbody>\n";
	echo "	<tfoot>\n";
	echo "		<tr>\n";
	echo "			<td colspan=\"" .$colSpan ."\">";
	echo "<span class=\"tableTextLeft\">Click a Pilots ID for additional pilot information and PIREP viewing/editing.";

	if ( $bFlightTracking )
		echo "<br />Click on the Online icons for near real time pilot flight tracking.";

	echo "</span></td>\n";
	echo "		</tr>\n";
	echo "	</tfoot>\n";
	echo "</table>\n";
}

//
// Display Month Totals table
//
function totalsTable($monthTotalsArray, $yearTotalsArray)
{
	echo "<table class=\"table_loa\">\n";
	echo "	<caption>Totals</caption>\n";
	echo "	<thead><tr><th></th><th>Active Pilots</th><th>PIREPS</th><th>PAX</th><th>Cargo<br/>lbs</th><th>Fuel<br/>lbs</th><th><br />Hrs</th></thead>\n";
	echo "	<tbody>\n";
	echo "		<tr>\n";
	echo "			<td class=\"tableTextCenter\">Month</td>\n";
	echo "			<td class=\"tableTextCenter\">"    .$monthTotalsArray["PILOTS"]      ."</td>\n";
	echo "			<td class=\"tableTextRight\">"     .$monthTotalsArray["PIREPS"]      ."</td>\n";
	echo "			<td class=\"tableTextRight\">"     .$monthTotalsArray["PAX"]         ."</td>\n";
	echo "			<td class=\"tableTextRight\">"     .$monthTotalsArray["CARGO"]       ."</td>\n";
	echo "			<td class=\"tableTextRight\">"     .$monthTotalsArray["FUEL_BURNED"] ."</td>\n";
	echo "			<td class=\"tableTextRightRedB\">" .$monthTotalsArray["FLIGHT_HRS"]  ."</td>\n";
	echo "		</tr>\n";
	echo "		<tr>\n";
	echo "			<td class=\"tableTextCenter\">Year</td>\n";
	echo "			<td class=\"tableTextCenter\">-</td>\n";
	echo "			<td class=\"tableTextRight\">"     .$yearTotalsArray["PIREPS"]      ."</td>\n";
	echo "			<td class=\"tableTextRight\">"     .$yearTotalsArray["PAX"]         ."</td>\n";
	echo "			<td class=\"tableTextRight\">"     .$yearTotalsArray["CARGO"]       ."</td>\n";
	echo "			<td class=\"tableTextRight\">"     .$yearTotalsArray["FUEL_BURNED"] ."</td>\n";
	echo "			<td class=\"tableTextRightRedB\">" .$yearTotalsArray["FLIGHT_HRS"]  ."</td>\n";
	echo "		</tr>\n";
	echo "	</tbody>\n";
	echo "	<tfoot><tr><td colspan=\"7\">&nbsp;</td></tr></tfoot></table>\n";
	echo "<p>&nbsp;</p>\n";

}

//
// Begin LOA Pilots Table
//
function loaPilotsTableBegin()
{
	echo "	<table class=\"table_loa\">\n";
	echo "	<caption>Pilots on Leave of Absence</caption>\n";
	echo "	<thead>\n";
	echo "		<tr>\n";
	echo "			<th>Pilot Name</th><th>Pilot<br />ID</th><th>Rank</th><th>A/C<br />Hrs</th><th>Helo<br />Hrs</th><th>Total<br />Hrs</th>\n";
	echo "		</tr>\n";
	echo "	</thead>\n";
	echo "	<tbody>\n";
}

//
// Add a row of Pilot information to the LOA Pilots Table
//
function loaPilotsTableAddRecord($pilotName, $pilotID, $rankGfx, $acHrs, $heloHrs, $totalHrs)
{
	echo "	<tr>\n";
	echo "		<td>" .$pilotName ."</td>\n";
	echo "		<td>" .$pilotID ."</td>\n";
	echo "		<td>" .$rankGfx ."</td>\n";
	echo "		<td class=\"tableTextRight\">" .$acHrs ."</td>\n";
	echo "		<td class=\"tableTextRight\">" .$heloHrs ."</td>\n";
	echo "		<td class=\"tableTextRightRedB\">" .$totalHrs ."</td>\n";
	echo "	</tr>\n";
}

//
// End of LOA Pilots Table
//
function loaPilotsTableEnd()
{
	echo "	</tbody>\n";
	echo "	<tfoot>\n";
	echo "		<tr>\n";
	echo "			<td colspan=\"6\">&nbsp;</td>\n";
	echo "		</tr>\n";
	echo "	</tfoot>\n";
	echo "	</table>\n";
	echo "<p>&nbsp;</p>\n";
}

//
//
//
function inactivePilotsTableBegin()
{
	echo "	<table class=\"table_loa\">\n";
	echo "	<caption>Inactive Pilots</caption>\n";
	echo "	<thead>\n";
	echo "		<tr>\n";
	echo "			<th>Pilot Name</th><th>Pilot<br />ID</th><th>Rank</th><th>A/C<br />Hrs</th><th>Helo<br />Hrs</th><th>Total<br />Hrs</th>\n";
	echo "		</tr>\n";
	echo "	</thead>\n";
	echo "	<tbody>\n";
}

//
//
//
function inactivePilotsTableAddRecord($pilotName, $pilotID, $rankGfx, $acHrs, $heloHrs, $totalHrs)
{
	echo "	<tr>\n";
	echo "		<td>" .$pilotName ."</td>\n";
	echo "		<td>" .$pilotID ."</td>\n";
	echo "		<td>" .$rankGfx ."</td>\n";
	echo "		<td class=\"tableTextRight\">" .$acHrs ."</td>\n";
	echo "		<td class=\"tableTextRight\">" .$heloHrs ."</td>\n";
	echo "		<td class=\"tableTextRightRedB\">" .$totalHrs ."</td>\n";
	echo "	</tr>\n";
}

//
//
//
function inactivePilotsTableEnd()
{
	include('include/config.php');

	echo "	</tbody>\n";
	echo "	<tfoot>\n";
	echo "		<tr>\n";
	echo "			<td colspan=\"6\"><span class=\"tableTextLeft\">Inactive pilots wil be retained for " .$noFlyDaysDelete ." days before deletion.</span></td>\n";
	echo "		</tr>\n";
	echo "	</tfoot>\n";
	echo "	</table>\n";
	echo "	<p>&nbsp;</p>\n";
}

?>
